<?php

namespace app\admin\controller;
use think\facade\Db;
use think\facade\View;

class Shipping extends Base{

    /**
     * 快递公司列表
     * @return mixed
     */
    public function index(){
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            $count = Db::name('shipping')->alias('p')->where($map)->count();

            $field = 'p.*';
            $list = Db::name('shipping')
                ->alias('p')
                ->field($field)
                ->where($map)
                ->order('p.id desc ')
                ->page($page,$limit)
                ->select();
            if($list){
                foreach($list as $k=>$v){
                }
            }

            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }

        return View::fetch();


    }

    /**
     * 快递公司详情页
     * @return mixed
     */
    public function info()
    {
        $shipping_id = input('id/d');
        if ($shipping_id) {
            $shipping = Db::name('shipping')->where(['id'=>$shipping_id])->find();
            if(empty($shipping)){
                $this->error('没有找到相应记录');
            }
            View::assign('info', $shipping);
        }
        View::assign('express_switch',tpCache('express.express_switch'));
        return View::fetch();
    }

    /**
     * 添加和更新快递公司
     */
    public function save()
    {
        $params = input();
        $validate = Loader::validate('Shipping');
//        if (!$validate->batch()->check($data)) {
//            $this->ajaxReturn(['status' => 0, 'msg' => '操作失败', 'result' => $validate->getError()]);
//        }
        $data = [];
        $data['shipping_name'] = $params['shipping_name'];
        $data['shipping_code'] = $params['shipping_code'];
        $data['is_open'] = $params['is_open'];
        if(!empty($params['id'])){
            Db::name('shipping')->where('id',$params['id'])->update($data);
        }else{
            Db::name('shipping')->where('id',$params['id'])->insert($data);
        }
        return json(['status' => 1, 'msg' => '操作成功', 'result' => '']);

    }

    /**
     * 删除快递公司
     * @throws \think\Exception
     */
    public function delete()
    {
        $ids = input('ids/a');
        Db::name('shipping')->where(['id'=>['in',$ids]])->delete();
        return json(['status' => 1, 'msg' => '删除成功', 'result' => '']);
    }
}